package com.hcbd.tdwt.ui.xingyu.fragment;
import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.widget.EditText;
import android.widget.TextView;


import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.BaseFragment;
import com.hcbd.tdwt.base.BeidouBoxParams;
import com.pangu.bdsdk2021.bluetooth.BluetoothManage;
import com.pangu.bdsdk2021.entity.terminalone.BDICInfo;
import com.pangu.bdsdk2021.entity.terminalone.BDSOSInfo;
import com.pangu.bdsdk2021.entity.terminalthree.BDSXXInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDDVXInfo;
import com.pangu.bdsdk2021.terminal.TerminalSendManager;
import com.pangu.bdsdk2021.util.BDProtocol_Level_2;
import com.pangu.bdsdk2021.util.BdCommonMethod;


import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.MessageFormat;

import static java.lang.Thread.sleep;

@SuppressLint("NonConstantResourceId")
@EFragment(R.layout.fragment_bd_device_sos)
public class BdDeviceSendSosFragment extends BaseFragment {
    @ViewById
    TextView frequencyTipsTV;
    @ViewById
    EditText frequencyET,sosContentET,centerNumber;
    @ViewById
    TextView startBtn;
    private String text="当前北斗卡服务频度为";
    Drawable ovaBlueDrawable;
    Drawable ovaGrayDrawable;
    Drawable ovaRedDrawable;
    private boolean isOpen=false;
    @AfterViews
    protected void init() {
        initMsgRegister();
        ovaBlueDrawable=getResources().getDrawable(R.drawable.ova_light_blue_bg);
        ovaRedDrawable=getResources().getDrawable(R.drawable.ova_red_bg2);
        ovaGrayDrawable=getResources().getDrawable(R.drawable.ova_gray_bg);
        frequencyTipsTV.setText(MessageFormat.format("{0}{1}秒", text, BeidouBoxParams.sentWaitSec));
        initData();
    }

    @Override
    public void onSwitch() {
        super.onSwitch();
        if(BeidouBoxParams.isBoxConnectNormal&&startBtn!=null){
            adapterHandler.sendEmptyMessageDelayed(2, 1000);
        }
    }

    public void initData(){
        if(BeidouBoxParams.isBoxConnectNormal){
            startBtn.setClickable(true);
            startBtn.setText("启动");
            startBtn.setBackground(ovaBlueDrawable);
            adapterHandler.sendEmptyMessage(2);

        }else{
            startBtn.setClickable(false);
            startBtn.setText("未连接");
            startBtn.setBackground(ovaGrayDrawable);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 2)
    public void eventBus(BDSOSInfo bdsosInfo){
        frequencyET.setText(String.valueOf(bdsosInfo.frequent));
        centerNumber.setText(String.valueOf(bdsosInfo.centerNumber));
        adapterHandler.sendEmptyMessageDelayed(1, 500);
    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 2)
    public void eventBus(BDDVXInfo bddvxInfo){
        if(bddvxInfo.md==1){//进入SOS模式
            setActivatedButton();
        }else{//正常模式
            setOldButton();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 2)
    public void eventBus(BDICInfo bdicInfo){
        setStartBtnStatus();
    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 2)
    public void eventBus(BDSXXInfo bdsxxInfo){
        setStartBtnStatus();
    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 2)
    public void eventBus(String bleContent){
        if("onConnectError".equals(bleContent)){
            setStartBtnStatus();
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 2)
    public void eventBus(BDSOSInfo.SwitchInfo switchInfo){
        if(switchInfo.result==0){//进入SOS模式
            setActivatedButton();
            toast("进入sos成功！");
        }else{//正常模式
            if(switchInfo.result==1){
                toast("无上报中心号码,进入SOS失败！");
                setOldButton();
            }else if(switchInfo.result==2){
                toast("系统已工作在SOS模式");
            }else if(switchInfo.result==3){
                toast("未读取到北斗卡，SOS无法工作");
                setOldButton();
            }
        }
    }




    public void setStartBtnStatus(){
        if(BeidouBoxParams.isBoxConnectNormal){
            startBtn.setClickable(true);
            startBtn.setText("启动");
            startBtn.setBackground(ovaBlueDrawable);
            adapterHandler.sendEmptyMessage(2);
        }else{
            startBtn.setClickable(false);
            startBtn.setText("未连接");
            startBtn.setBackground(ovaGrayDrawable);
        }
    }






    public void setOldButton(){
        isOpen=false;
        startBtn.setClickable(true);
        startBtn.setText("启动");
        startBtn.setBackground(ovaBlueDrawable);
    }
    public void setActivatedButton(){
        isOpen=true;
        startBtn.setClickable(true);
        startBtn.setText("已启动");
        startBtn.setBackground(ovaRedDrawable);
    }
    /**
     * 刷新UI
     */
    @SuppressLint("HandlerLeak")
    private Handler adapterHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    TerminalSendManager.getInstance().level_two_getCCDVQ();//获取当前工作模式
                 /*   String command = BdCommonMethod.castHanziStringToHexString("$CCDVQ,*45")+"0D0A";
                    BluetoothManage.getInstance().write(command);*/
                    break;
                case 2:
                    TerminalSendManager.getInstance().level_two_getSHMInfo();//获取SOS信息
                    break;
                case 3:
                   if(isOpen){
                       TerminalSendManager.getInstance().level_two_openSOS(0);
                    }else{
                       TerminalSendManager.getInstance().level_two_openSOS(1);
                    }
                   hideLoading();
                    break;
            }
        }
    };

    @Click
    void startBtn(){
        if(!BeidouBoxParams.isBoxConnectNormal){
            toast("请连接设备!");
            return;
        }
         String number=getTextStr(centerNumber);
         if("".equals(number)){
             toast("请输入中心号码!");
             return;
         }
        String frequency=getTextStr(frequencyET);
        if("".equals(frequency)){
            toast("请输入报警频度!");
            return;
        }
        showLoading("发送中");
        TerminalSendManager.getInstance().level_two_setSOSInfo(Integer.parseInt(number),Integer.parseInt(frequency),"");
        adapterHandler.sendEmptyMessageDelayed(3, 1500);
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

}
